package jdmx.framework.service;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import jdmx.library.info.DbInfo;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

public class ServiceConfiguration {	
	public ArrayList<DbInfo> dbInfos = null;		
	public String boPrefix;
	public String boSuffix;
	public String btPrefix;
	public String btSuffix;
	public String daoPrefix;
	public String daoSuffix;
	public String ormPrefix;
	public String ormSuffix;
	
	public ServiceConfiguration() {
		dbInfos = new ArrayList<DbInfo>();
	}
	
	private void readDbInfo() {
		String fileName = "./conf/dbinfo.xml";
		File file = new File(fileName);
		SAXBuilder parser = new SAXBuilder();
		Document xmlDocument = null;
		try {
			xmlDocument = parser.build(file);
		} catch (JDOMException e1) {
		} catch (IOException e1) {
		}
		if (xmlDocument == null) return;
		
		Element rootElement = xmlDocument.getRootElement();
		@SuppressWarnings("unchecked")
		List<Element> xmlDbInfos = rootElement.getChildren("dbInfo");
		for (Element xmlDbInfo : xmlDbInfos) {
			DbInfo dbInfo = new DbInfo();
			dbInfo.name = xmlDbInfo.getChild("name").getText();
			dbInfo.dbmsType = xmlDbInfo.getChild("dbmsType").getText();
			dbInfo.dbServer = xmlDbInfo.getChild("dbServer").getText();
			dbInfo.dbPort = xmlDbInfo.getChild("dbPort").getText();
			dbInfo.dbName = xmlDbInfo.getChild("dbName").getText();
			dbInfo.dbUsername = xmlDbInfo.getChild("dbUsername").getText();
			dbInfo.dbPassword = xmlDbInfo.getChild("dbPassword").getText();
			dbInfos.add(dbInfo);
		}
	}
	
	public void readLocalConfiguration() {
		readDbInfo();
	}
}
